<?php
class Acl_resource_model extends CI_Model {
	
	private $tbl_data1= 'ACL_RESOURCES';
 
	public function __construct()
	{
		parent::__construct();
		 
	}
	function list_all(){
		//$this->db->order_by('id','asc');
		return "SELECT * FROM  ACL_RESOURCES";
	}
	
	function count_all(){
		return $this->db->count_all($this->tbl_data1);
	}
	function get_by_id($id){
		$this->db->where('recode', $id);
		return $this->db->get($this->tbl_data1)->row();
	 
	}
	function get_by_name($id){
		$this->db->where('controller', $id);
		return $this->db->get($this->tbl_data1)->row();
	 
	}
	function get_all_resource($parent_id,$title,$results,$prefix=''){
		 $table='<table class="tbl">'."\n";
		 $table.="<tr><th class='tbl-header'>$title</th></tr>"."\n";
		 $tr='tbl-row-even';
		 $table.=$this->get_resource($parent_id,'',$tr,$results,$prefix);
		 $table.="</table>"."\n";
		 return $table;
	}
	
	function get_resource($parent_id,$space,$tr,$results,$prefix){
		$html='';
		foreach ($results as $row)
		 {
		 	 //print_r($row);
		  
		 	 if ($row->parent_id==$parent_id) 
		 	 {
		 	 	if ($row->controller=="#") $url=curPageURL();
		 	 	else $url=base_url()."index.php/".$prefix.$row->controller;
				$link='<a href="'.$url.'" />'.$row->name.'</a>';
				$html.="<tr class='$tr'><td class='tbl-cell tbl-controls'>".$space."<img src='".base_url()."assets/css/images/bullet_blue.gif'  /> ".$link."</td>";
				$html.=$this->get_resource($row->recode,$space."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",'tbl-row-odd',$results,$prefix);
			 	  //$table.=$this->get_resource($row->PARENT_ID,'',$cell,$resource);
		 	 }
		 }
		return $html;
	}
	function save($role){
		$this->db->insert($this->tbl_data, $role);
		return $this->db->insert_id();
	}
	
	function update($id, $role){
		$this->db->where('role_id', $id);
		$this->db->update($this->tbl_data, $role);
	}
	
	function get_report_content($id){
	     $html='<select id="month"><option>--</option></select>';
	     return $html;
	}
	
	function get_navigator($parent_id){
		$result='';
		$sql="SELECT A.* FROM  ACL_RESOURCES A  WHERE PARENT_ID='".$parentid."' ORDER BY ORDER_NO";
		$results=$this->db->query($sql)->result_array();
		if($results) $result=get_navigator($results->PARENT_ID);
	    else $result='<a href="'.base_url()."index.php/".$results->CONTROLLER.'" />'.$row["NAME"].'</a> >';
	    return $result;
	}
	
	function delete($id){
		$this->db->where('role_id', $id);
		$this->db->delete($this->tbl_data);
	}
	
	function get_paged_list($limit = 10, $offset = 0){
		$this->db->order_by('role_id','asc');
		return $this->db->get($this->tbl_data, $limit, $offset);
	}
}
?>